package org.goyo.main.services.base.impl;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.base.ISupplierClassificationDao;
import org.goyo.main.domain.entity.base.SupplierClassification;
import org.goyo.main.services.base.ISupplierClassificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;
@Service
@Transactional
public class SupplierClassificationServiceImpl implements ISupplierClassificationService {
  protected Logger logger = LoggerFactory.getLogger(this.getClass());
  @Resource
  private ISupplierClassificationDao supplierClassificationDao;
  @Override
  public String saveOrUpdateInfo(SupplierClassification info) throws DBException,
      ServiceException {
    String pid = info.getParentId();
    if(!StringUtils.isEmpty(pid)){
        supplierClassificationDao.updateByHql("update SupplierClassification set leaf=? where id=? ", new Object[]{false,pid});
      }
        String uuid = null;
        if(info.getUuid() == null || "".equals(info.getUuid())){
            info.setUuid(null);
            uuid = supplierClassificationDao.save(info);
        }else{
          uuid = info.getUuid();
          if(supplierClassificationDao.countByHql("select count(*) from SupplierClassification where parentId ='"+uuid+"'")>0){
            info.setLeaf(false);
          }else{
            info.setLeaf(true);
          }
          supplierClassificationDao.update(info);
        }
        return uuid;}

  @Override
  public void delByUuids(Object[] uuids) throws DBException, ServiceException {
    for(Object uuid : uuids){
      SupplierClassification supplierClassification  = supplierClassificationDao.get(SupplierClassification.class, uuid.toString());
      supplierClassificationDao.delete(supplierClassification);
  }
}

  @Override
  public SupplierClassification findByUuid(String uuid) throws DBException, ServiceException {
    return supplierClassificationDao.get(SupplierClassification.class, uuid);
    }

  @Override
  public Pager<SupplierClassification> findByCondition(String where, Pager pager)
      throws DBException, ServiceException {
    return  supplierClassificationDao.findByCond(where, pager);
    }

  @Override
  public List<SupplierClassification> getAllSupplierClassifications() throws DBException,
      ServiceException {
    List<SupplierClassification> list = supplierClassificationDao.getAllSupplierClassifications();
    return list;
  }

  @Override
  public String getSupplierClassificationSerialNumber() throws DBException, ServiceException {
    int i =0;
    try {
      List list =  supplierClassificationDao.findObjects("select max(CAST(number AS  int)) from SupplierClassification", null, null);
      if(null!=list){
       i =(Integer)list.get(0)+1;
      }
    } catch (Exception e) {
      i=0;
      logger.error(e.getMessage(),e);
    }
    String pattern="0000";
    java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
    return df.format(i)+"";
  }

}
